/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gblend.result;
/*
* Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
*
* File name: Createdot.java
*
* Abstract: Create the DOT format file for the selected result graph
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        Dec.08,2009
*
*/
/**
 *
 * @author cjjin
 */
import gblend.adjlistgraph.ALGraph;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import java.sql.*;
import gblend.db.DatabaseInfo;
import gblend.frequentindex.Vertex;
import gblend.exactquery.MatchedGraph;

public class Createdot {

    private static ALGraph g_display;

    public  void createGraph(DatabaseInfo dbinfo, ALGraph graph) throws ClassNotFoundException, SQLException
    {
        int nodenum = graph.getNnum();
        g_display = new ALGraph(nodenum);
        g_display.setNnum(nodenum);
    //	String reallabels[]=dbinfo.getLabels();

        for(int i = 0; i< nodenum; i++)
        {
          String l = graph.getNode(i).getLabel();
        //  String label = reallabels[Integer.parseInt(l)]+"_"+i;
           String label = "L"+l+"Id"+i;
          Vertex node = new Vertex();
          node.setLabel(label);
          g_display.addnode(node);

        }

        for(int i = 0; i< nodenum; i++)
        {
          for(int j = 0; j< nodenum; j++)
          {
             if(graph.getEdge(i, j)==1)
             {
                g_display.addEdge(i,j);
             }
          }
        }

        g_display.setEnum(graph.getEnum());
        g_display.setAllGraphid(graph.getGraphidAsSet());

    }


    //formulate the dot file
    public File formatDotFile(MatchedGraph mg)
    {
        String output="graph.dot";
        FileOutputStream outStream;
	PrintStream p;
	File fout=new File(output);

        try {
    		outStream=new FileOutputStream(fout,false);
		// Connect print stream to the output stream
		p = new PrintStream( outStream );
			
                p.println("graph \"result\" {");
                p.println("graph [fontname = \"Helvetica-Oblique\", fontsize = 20,label = \"\\n\\nGraph "+
                        g_display.getGraphidAsEntry().toString()+" ("+g_display.getNnum()+","+g_display.getEnum()+")"+"\",size = \"4,4\" ];");
                p.println("node [label = \"\\N\",shape = box, sides = 4, color = cadetblue1," +
                        "style = filled,fontname = \"Helvetica-Outline\" ];");
	
                for(int i=0; i< g_display.getNnum(); i++)
                {
                     if(mg.getNodeset().contains(i))
                     {
                            p.println(g_display.getNode(i).getLabel()+" [color= orange];");
                     }
                     else
                     {
                            p.println(g_display.getNode(i).getLabel()+";");
                     }
                }

                for(int i=0; i< g_display.getNnum(); i++)
                {
                    for(int j=i+1; j< g_display.getNnum(); j++)
                    {
                        if(g_display.getEdge(i, j)==1)
                        {
                              p.println(g_display.getNode(i).getLabel()+" -- "+g_display.getNode(j).getLabel()+";");
                        }
                    }
                }

                p.println("}");
                p.close();

            }
            catch (IOException e)
            {
		// TODO Auto-generated catch block
                e.printStackTrace();
            }
	
		return fout;	
	}

}
